// pages/cardMyDetail/cardMyDetail.js
var api = require("../../utils/api.js");
var util = require("../../utils/util.js");
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    controlDome: true, //地址弹窗
    codeDemo:true, //二维码弹窗
    description: true, //说明弹窗
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
          id: options.id
        })
      }
    })
    
    
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.addressList();
    this.cardDetail();
    this.setData({
      "controlDome": true
    })

    wx.hideShareMenu()
  },
  //卡券详情
  cardDetail:function(){
    var params = new Object()
    var that = this;
    params.id=this.data.id;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'couponsbag/info',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            cardDetail: res.data.data,
            coupons_code: res.data.data.code,
            descriptionText: res.data.data.description
          })
          that.getShareDetail();
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  //地址请求
  addressList: function () {
    var params = new Object()
    var that = this;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'address/list',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            address: res.data.data.address_list
          })
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  controlDomeShow: function () {
    this.setData({
      "controlDome": false
    })
  },
  controlDomeHide: function () {
    this.setData({
      "controlDome": true
    })
  },
  addAddress:function(){
    wx.navigateTo({
      url: '../addAddress/addAddress'
    })
  },
  radioChange: function (e) {
    var id = e.detail.value
    var that = this;
    var list = that.data.address;
    for (var i = 0; i < list.length; i++) {
      // console.log(list[i].aid);
      if (list[i].aid == id) {
        var params = {
          aid: id,
          username: list[i].username,
          mobile: list[i].mobile,
          province: list[i].province.name,
          town: list[i].town.name,
          city: list[i].city.name,
          address: list[i].address,
          is_default: 1,
          street: list[i].street.name
        }
        // console.log(params);
        api.POST({
          params: params,
          url: app.globalData.base_url + 'address/modify',
          success: function (res) {
            if (util.isEmpty(res.data.code)) {
              that.addressList();
            }
          },
          fail: function (fail) {
            util.showError("登录失败，请检查网络");

          }
        })

      }
    }
  },
//配送上门
  useCard:function(){
    var params = new Object()
    var that = this;
    var address = this.data.address
    for (var i = 0; i < address.length; i++) {
      if (address[i].is_default == 1) {
        params.aid = address[i].aid
      }
    }

    if (params.aid == undefined || params.aid == '') {
      util.showError("请选择收货地址");
      return false;
    }

    params.coupons_code = this.data.coupons_code;

    wx.showModal({
      title: '提示',
      content: '是否确认提交',
      success: function (res) {
        if (res.confirm) {
          api.POST({
            params: params,
            url: app.globalData.base_url + 'couponsorder/createMailOrder',
            success: function (res) {
              if (util.isEmpty(res.data.code)) {
                var oid = res.data.data.oid;
                var typeText = res.data.data.type
                wx.showModal({
                  title: '提示',
                  confirmText:'查看详情',
                  cancelText:'返回卡包',
                  content: '提交成功',
                  success: function (res) {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '../orderInfo/orderInfo?oid=' + oid + '&&type=' + typeText
                      })
                    } else if (res.cancel) {
                      wx.navigateTo({
                        url: '../cardMy/cardMy'
                      })
                    }
                  }
                })
              }else{
                util.showError(res.data.message);
              }
            },
            fail: function (fail) {
              util.showError("登录失败，请检查网络");

            }
          })
        } else if (res.cancel) {
        
        }
      }
    })

    

  },

  //修改地址
  editorAddress: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/addAddress/addAddress?aid=' + id
    })
  },
  //删除地址
  deleteAddress: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    api.POST({
      params: { aid: id },
      url: app.globalData.base_url + 'address/del',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          util.showStatus("删除成功")
          that.addressList();
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },

  //立即使用
  useCardNow:function(){
    var params = new Object()
    var that = this;
    params.coupons_code = this.data.coupons_code;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        params.lat = res.latitude
        params.lng = res.longitude
        api.POST({
          params: params,
          url: app.globalData.base_url + 'couponsbag/useInfo',
          success: function (res) {
            if (util.isEmpty(res.data.code)) {
              var code_img = res.data.data.info.code_img;
              that.setData({
                codeDemo: false,
                code_img: code_img
              })
            }
          },
          fail: function (fail) {
            util.showError("登录失败，请检查网络");

          }
        })
      }
    })



  },

  closeCodeDemo:function(){
    this.setData({
      codeDemo:true
    })
  },

  description:function(){
    this.setData({
      description:false
    })
  },
  closeDescription:function(){
    this.setData({
      description: true
    })
  },

  //回购跳转
  repo:function(){
    var id = this.data.id;
    wx.navigateTo({
      url: '../applicationRepurchase/applicationRepurchase?id=' + id
    })
  },

  // 获取分享详情
  getShareDetail:function(){
    var params = new Object()
    var that = this;
    params.coupons_code = this.data.coupons_code;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'couponsbag/getShareLink',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            linkDetail: res.data.data,
            shareStatus: res.data.data.status
          })
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  onShareAppMessage: function (res) {
    var linkCode = this.data.linkDetail.linkCode;
    var name = this.data.linkDetail.share_title;
    var image = this.data.linkDetail.share_image;
    var that = this;
    if (res.from === 'button') {
      return {
        title: name,
        imageUrl: image,
        path: '/pages/cardMyDetailGive/cardMyDetailGive?linkCode=' + linkCode,
        success: function (res) {
          that.shareSuccess();
        },
        fail: function (res) {
          // 转发失败
        }
      }
    }
    
  },
  //分享成功回调
  shareSuccess:function(){
    var params = new Object()
    var that = this;
    params.id = this.data.linkDetail.id;
    params.linkCode = this.data.linkDetail.linkCode;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'couponsbag/shareSuccess',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          util.showStatus(res.data.message);
          setTimeout(function(){
            wx.navigateBack({
              delta: 1
            },2000)
          })
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },

  //跳转门店地址
  shopsAddress:function(){
    var coupons_code = this.data.coupons_code;
    wx.navigateTo({
      url: '../shopAddress/shopAddress?coupons_code=' + coupons_code
    })
  },


})